package cn.virde.freemaker;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;

import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

/**
 * Description
 *
 * @Author: SunAo
 * @Date: 2020/12/7 21:49
 */
public class FreemarkTest {

    static final Configuration configuration = new Configuration(Configuration.VERSION_2_3_30);
    static{
        try {
            configuration.setDirectoryForTemplateLoading(new File(FreemarkTest.class.getClassLoader().getResource("template").getPath()));
            configuration.setDefaultEncoding("UTF-8");
            configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    public static void main(String[] args) throws IOException, TemplateException {
        // Create the root hash
        Map<String, Object> root = new HashMap<>();
        root.put("name", "Big Joe");
        root.put("password", "new passwords");

        Template temp = configuration.getTemplate("hello.ftl");
        Writer out = new OutputStreamWriter(System.out);
        temp.process(root, out);
    }
}
